package Top200;

/**
 * @author zhangmin
 * @create 2022-02-22 10:07
 * 对于第i为和第j位的相乘结果是累计在，结果的第i+j和i+j+1位上的。
 */
public class multiply43 {
    public String multiply(String num1, String num2) {
        int m=num1.length(),n=num2.length();
        int[] res=new int[m+n];
        for (int i = m-1; i >=0 ; i--) {
            for (int j = n-1; j >=0 ; j--) {
                int mul=(num1.charAt(i)-'0')*(num2.charAt(j)-'0');
                int sum=mul+res[i+j+1];
                res[i+j]+=sum/10;
                res[i+j+1]=sum%10;
            }
        }
        StringBuffer sb=new StringBuffer();
        int i = 0;
        while (i<res.length&&res[i]==0) i++;
        for (; i < res.length; i++) {
            sb.append(res[i]);
        }
        return sb.toString().length()==0?"0":sb.toString();
    }
}
